package com.sld.concrete.device.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.sld.concrete.device.domain.InfoDevice;
import com.sld.concrete.device.service.IInfoDeviceService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 设备管理Controller
 * 
 * @author topviewers
 * @date 2022-03-13
 */
@Controller
@RequestMapping("/device/device")
public class InfoDeviceController extends BaseController
{
    private String prefix = "device/device";

    @Resource
    private IInfoDeviceService infoDeviceService;

    @RequiresPermissions("device:device:view")
    @GetMapping()
    public String device()
    {
        return prefix + "/device";
    }

    /**
     * 查询设备管理列表
     */
    @RequiresPermissions("device:device:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(InfoDevice infoDevice)
    {
        startPage();
        List<InfoDevice> list = infoDeviceService.selectInfoDeviceList(infoDevice);
        return getDataTable(list);
    }

    /**
     * 导出设备管理列表
     */
    @RequiresPermissions("device:device:export")
    @Log(title = "设备管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(InfoDevice infoDevice)
    {
        List<InfoDevice> list = infoDeviceService.selectInfoDeviceList(infoDevice);
        ExcelUtil<InfoDevice> util = new ExcelUtil<InfoDevice>(InfoDevice.class);
        return util.exportExcel(list, "设备管理数据");
    }

    /**
     * 新增设备管理
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存设备管理
     */
    @RequiresPermissions("device:device:add")
    @Log(title = "设备管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(InfoDevice infoDevice)
    {
        return toAjax(infoDeviceService.insertInfoDevice(infoDevice));
    }

    /**
     * 修改设备管理
     */
    @RequiresPermissions("device:device:edit")
    @GetMapping("/edit/{devId}")
    public String edit(@PathVariable("devId") String devId, ModelMap mmap)
    {
        InfoDevice infoDevice = infoDeviceService.selectInfoDeviceByDevId(devId);
        mmap.put("infoDevice", infoDevice);
        return prefix + "/edit";
    }


    /**
     * 绑定车牌信息
     */
    @GetMapping("/bind/{devId}")
    public String bind(@PathVariable("devId") String devId, ModelMap mmap) {
        mmap.put("devId", devId);
        return prefix + "/bind";
    }

    @RequiresPermissions("device:device:edit")
    @Log(title = "设备绑车牌", businessType = BusinessType.UPDATE)
    @PostMapping("/bindPlate")
    @ResponseBody
    public AjaxResult bindPlate(String devId, String plateNo) {
        try {
            return toAjax(infoDeviceService.updateInfoDeviceBindPlate(devId, plateNo));
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    @RequiresPermissions("device:device:edit")
    @Log(title = "解绑车牌", businessType = BusinessType.UPDATE)
    @PostMapping("/unBind")
    @ResponseBody
    public AjaxResult bindPlate(String devId) {
        return toAjax(infoDeviceService.deleteBindRelation(devId));
    }


    /**
     * 修改保存设备管理
     */
    @RequiresPermissions("device:device:edit")
    @Log(title = "设备管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(InfoDevice infoDevice)
    {
        return toAjax(infoDeviceService.updateInfoDevice(infoDevice));
    }

    /**
     * 删除设备管理
     */
    @RequiresPermissions("device:device:remove")
    @Log(title = "设备管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(infoDeviceService.deleteInfoDeviceByDevIds(ids));
    }
}
